package cn.neusoft.service.impl;

import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.po.User;
import cn.neusoft.mapper.UserMapper;
import cn.neusoft.service.IUserService;
import cn.neusoft.vo.loginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hxj
 * @since 2023-12-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    /**
     * 登录
     * @param loginVo
     * @return
     */
    public User login(loginVo loginVo) {
        //参数校验
        if (!StringUtils.hasText(loginVo.getAccount())){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        if (!StringUtils.hasText(loginVo.getPassword())){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        //组合搜索条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", loginVo.getAccount());
        queryWrapper.or();
        queryWrapper.eq("email", loginVo.getAccount());
        queryWrapper.or();
        queryWrapper.eq("username", loginVo.getAccount());

        //通过以上条件   where phone=xxx or email=xx or username=xx
        User user = getOne(queryWrapper);

        //没有对应的账号  报错
        if (user==null){
            throw new AppException(AppExceptionCodeMsg.USERNAME_NOT_EXISTS);
        }

        //查看状态
        if (user.getState()==0){
            throw new AppException(12002,"用户未激活");
        }

        if (user.getState()==2){
            throw new AppException(12003,"用户已禁用");
        }

        //最终返回找到的用户对象
        return user;


    }
}
